package cn.net.greenet.iprange.info;

import cn.net.greenet.db.bean.IprangeuserRange;
import com.ambimmort.umessage.UcType;
import com.ambimmort.umessage.exceptions.UcTypeException;
import com.ambimmort.umessage.uc.outgoing.uvm.x82;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 通过一个IP地址查询到用户信息，并且生成0x82对象
 *
 * @author liangdelin
 */
public class SearchIpUserRelation {

    public static List<byte[]> search(String ip) throws UcTypeException, UnsupportedEncodingException {
        if (GetIpRangeUserInfo.getIpRange().isEmpty()) {
            GetIpRangeUserInfo.setIpRange();
        }
        Map<String, List<IprangeuserRange>> ipRange = GetIpRangeUserInfo.getIpRange();
        return getX82Message(ipRange, ip);
    }

    public static List<byte[]> getX82Message(Map<String, List<IprangeuserRange>> ipRange, String ip) throws UcTypeException, UnsupportedEncodingException {
        List<byte[]> data = new ArrayList<>();  //return message
        int ipv4 = 0;
        int prefix = 0;
        x82 msg0x82 = null;
        List<IprangeuserRange> ip_range_list = null;
        List<x82.UserIP82> ip_Segments = null;
        for (String key : ipRange.keySet()) {
            ip_range_list = ipRange.get(key);
            msg0x82 = new x82();
            msg0x82.setBind_Action(UcType.newUINT1(1));
            msg0x82.setUserName(UcType.newUString_UINT1(key));
            ip_Segments = new ArrayList<>();
            for (IprangeuserRange ii : ip_range_list) {
                ipv4 = ii.getIpv4();
                prefix = ii.getPrefix();
                if (judgeRight(IpToIntTools.getIPIntScope(IpToIntTools.intToIp(ii.getIpv4()) + "/" + ii.getPrefix()), IpToIntTools.ipToInt(ip))) {
                    x82.UserIP82 xx = new x82.UserIP82();
                    System.out.println("ipv4"+ipv4);
                     System.out.println("prefix"+prefix);
                    xx.setUserIP(new UcType.IPv4(IpToIntTools.intToIp(ipv4)));
                    xx.setUserIP_Prefix(UcType.newUINT1(prefix));
                    ip_Segments.add(xx);
                }
            }
            if (!ip_Segments.isEmpty()) {
                msg0x82.setIp_Segments(ip_Segments);
                data.add(msg0x82.toBytes());
            }
        }
        return data;
    }

    /**
     * 判断IP地址是否命中
     *
     * @param iprange
     * @return
     */
    public static boolean judgeRight(int[] iprange, int b) {
        b = Math.abs(b);
        int start = Math.abs(iprange[0]);
        System.out.println("start" + start);
        int end = Math.abs(iprange[1]);
        System.out.println("end" + end);
        System.out.println("b" + b);
        if ((start <= b && b <= end) || (end <= b && b <= start)) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) throws UcTypeException, UnsupportedEncodingException {
        System.out.println(SearchIpUserRelation.search("192.168.1.2").size());
    }
}
